package com.scaffold;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import wangjg.commons.bean.BeanComparatorByPinyin;
import wangjg.commons.bean.BeanUtil;
import wangjg.commons.page.Page;
import wangjg.security.http.HttpLoginModule;

import com.scaffold.authority.entity.User;
import com.scaffold.commons.exception.BizRTException;
import com.scaffold.commons.json.JacksonUtil;
import com.scaffold.commons.spring.BasicAction;
import com.scaffold.commons.web.WebUtil;

public class BasicBizAction extends BasicAction {
	
	protected String getFailureResult(String message){
		return this.getResult("", message, null); 
	}
	
	protected String getSuccessResult(){
		return this.getResult("0000", null, null);
	}
	
	protected String getSuccessResult(Object data){
		return this.getResult("0000", null, data);
	}

	protected String getSuccessResult(List<?> list, boolean compress){
		return this.getResult("0000", null, (list.size()>1?jsonUtil.cutJsonList2(list):list));
	}
	
	private JacksonUtil jsonUtil = new JacksonUtil();
	
	protected String getPageResult(Page page, List<?> list){
		Map<String,Object> data = new HashMap<String,Object>();
		data.put("list", list);
		String pageTags = StringUtils.join(page.getPageTags(1, 3, 1),',');
		data.put("pageTags", pageTags);
		return this.getSuccessResult(data);
	}

	protected void checkOwner(Object entity) {
		if (entity == null) {
			throw new BizRTException();
		}
	}

	protected void setOwner(Object user) {
		
	}
	
	protected User currentUser(){
		User user = (User) this.getRequest().getSession().getAttribute(HttpLoginModule.USER_SESSION_KEY);
		return user;
	}
	
	protected void filter(List<?> beanList, String...pps){
		for(Object bean:beanList){
			this.filter(bean, pps);	
		}
	}
	
	protected void filter(Object bean, String...pps){
		BeanUtil.setNull(bean, pps);
	}
	
	/**
	 * 根据参数数组取得对象列表
	 * @param primaryKey 作为主要参数的参数名
	 * @param clazz 对象类型
	 * @return
	 */
	protected <T> List<T> getBeanList(String primaryKey, Class<T> clazz){
		List<T> list = new ArrayList<T>();
		
		List<Map<String, String>> maplist = WebUtil.getParameterMapList(this.getRequest(), primaryKey);
		try {
			for(Map<String, String> map:maplist){
				T bean = (T) clazz.newInstance();
				bindParams(bean, map, null, null);
				list.add(bean);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return list;
	}
	
	@SuppressWarnings("unchecked")
	protected <T> Map<String, T> getOptions(List<T> list, String displayProp){
		List<T> list2 = BeanUtil.copyList2(list, "id", displayProp);
		
		String orderBy = displayProp;
		Collections.sort(list2, new BeanComparatorByPinyin(orderBy, true));
		Map<String, T> map = (Map<String, T>) jsonUtil.cutJsonList2(list2);
		return map;
	}
	
	protected String getContextPath(){
		return this.getRequest().getContextPath();
	}

}
